package gouhuo.allbugs.top.controller;

import gouhuo.allbugs.top.common.BaseResponse;
import gouhuo.allbugs.top.common.BusinessException;
import gouhuo.allbugs.top.common.ErrorCode;
import gouhuo.allbugs.top.model.request.friends.FriendHandleRequest;
import gouhuo.allbugs.top.model.request.friends.FriendUpdateRequest;
import gouhuo.allbugs.top.model.vo.FriendsMsgVO;
import gouhuo.allbugs.top.model.vo.FriendsVO;
import gouhuo.allbugs.top.service.FriendsService;
import gouhuo.allbugs.top.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 好友
 */
@RestController
@RequestMapping("/friends")
public class friendsController {

    @Resource
    private FriendsService friendsService;

    @ApiOperation("发出好友申请")
    @PostMapping("/msg")
    public BaseResponse<Integer> friendMsg(@RequestBody FriendsMsgVO friendsMsgVO, HttpServletRequest request) {
        if (friendsMsgVO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long msg = friendsService.friendMsg(friendsMsgVO);
        if (msg != 1) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtil.success(200);

    }

    @ApiOperation("查询好友申请列表")
    @GetMapping("/msgList")
    public BaseResponse<List<FriendsMsgVO>> msgList(@RequestParam("userId") Long userId) {
        if (userId == null || userId == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<FriendsMsgVO> list = friendsService.msgList(userId);
        return ResultUtil.success(list);
    }

    @ApiOperation("处理好友申请")
    @PostMapping("/handle")
    public BaseResponse<Integer> handleMsg(@RequestBody FriendHandleRequest friendHandleRequest) {
        if (friendHandleRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long handle = friendsService.handleMsg(friendHandleRequest);
        if (handle != 1) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtil.success(200);
    }

    @ApiOperation("查询好友列表")
    @GetMapping("/list")
    public BaseResponse<List<FriendsVO>> friendList(@RequestParam("userId") Long userId) {
        if (userId == null || userId == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<FriendsVO> list = friendsService.friendList(userId);
        return ResultUtil.success(list);
    }

    @ApiOperation("修改好友备注")
    @PostMapping("/update")
    public BaseResponse<Integer> friendUpdate(@RequestBody FriendUpdateRequest friendUpdateRequest) {
        if (friendUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean update = friendsService.friendUpdate(friendUpdateRequest);
        if (!update) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "修改失败");
        }
        return ResultUtil.success(200);
    }

    @ApiOperation("删除好友")
    @PostMapping("/delete")
    public BaseResponse<Integer> friendDelete(@RequestBody FriendUpdateRequest friendUpdateRequest) {
        if (friendUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean delete = friendsService.friendDelete(friendUpdateRequest);
        if (!delete) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return ResultUtil.success(200);
    }

    @ApiOperation("获取对应好友的信息")
    @GetMapping("/isFriend")
    public BaseResponse<FriendsVO> isFriend(@RequestParam("userId") Long userId, @RequestParam("friendId") Long friendId) {
        if (userId == null || friendId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtil.success(friendsService.isFriend(userId, friendId));
    }

}
